<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * 商品管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$manage_op=array('start','off');
if(in_array($_RQ['op'],$manage_op)){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择商品！','error');
	}
}
$return['levels']=$this->getall('level',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
$return['levels']['0']=array('id'=>0,'title'=>'默认等级');
if($_RQ['op']=='list'){
	$params=array();
	$params['uniacid']=$_SESSION['uniacid'];
	if(!empty($_RQ['title'])){
		$return['params']['title']=$_RQ['title'];
		$goodsparams=array('uniacid'=>$_SESSION['uniacid']);
		$goodsparams['title like']='%'.trim($_RQ['title']).'%';
		$goodsids=pdo_getall('jy_weishop_goods',$goodsparams,'id','id');
		if($goodsids){
			$goodsids=array_keys($goodsids);
			$params['goodsid']=$goodsids;
		}else{
			$params['goodsid']=0;
		}
	}
	if(isset($_RQ['status'])){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$list=pdo_getslice('jy_psipingtuan_goods',$params,array($pindex,$psize),$total,$fields,'','star desc,id desc');
	if($list){
		foreach($list as &$item){
			$item['url']=custom_url('jy_psipingtuan/goods.detail//id='.$item['id'],true);
			$goods=pdo_get('jy_weishop_goods',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['goodsid']));
			$item['goodsprice']=$goods['price'];
			$item['title']=$goods['title'];
			$item['image']=$goods['image'];
			$item['endstatus']=$item['starttime']<time() ? $item['endtime']>time()? 1 : 2 : 0;
			$item['statustitle']=$item['starttime']<time() ? $item['endtime']>time()? '去拼团' : '已结束' : '未开始';
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'商品列表'),
			array('op'=>'edit','title'=>'添加商品'),
		),
		'list'=>array('data'=>'list','tip'=>'[温馨提示]:</br>1、不单独设置拼团信息的商品，将默认：设置--基本设置--里面默认设置！</br>2、只有成团后，未中奖的才有奖励。如果拼团失败，所有人都没有奖励的！不然平台会亏损的','edit'=>true,'list'=>array(
			array('name'=>'star','title'=>'星数_Star','type'=>'star','set'=>'star'),
			array('name'=>'title','title'=>'商品名称','type'=>'string'),
			array('name'=>'image','title'=>'图片','type'=>'image'),
			array('name'=>'goodsprice','title'=>'商品原价','type'=>'string'),
			array('name'=>'url','title'=>'二维码','type'=>'ewm'),
			array('name'=>'price','title'=>'拼团价格','type'=>'string'),
			array('name'=>'timeout','title'=>'组团限时/分钟','type'=>'string'),
			array('name'=>'teamnum','title'=>'成团人数','type'=>'string'),
			array('name'=>'lastnum','title'=>'中奖人数','type'=>'string'),
			array('name'=>'fan_fee','title'=>'失败奖励','type'=>'string'),
			array('name'=>'status','title'=>'是否启用','type'=>'switch','set'=>'set'),
			array('name'=>'url','title'=>'复制链接','type'=>'copy'),
		)),
		'search'=>array(
			array('name'=>'title','title'=>'商品名称','type'=>'string','width'=>'20'),
			array('name'=>'status','title'=>'是否启用','type'=>'select','options'=>'status','width'=>'20'),
		)
	);
	$return['status']=array(array('id'=>'0','title'=>'禁用'),array('id'=>'1','title'=>'启用'));
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='star'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('参数错误！','error');
	}
	$update=array();
	$post=get_postdata();
	if(isset($post['star'])){
		$update['star']=$post['star'];
	}
	if($update){
		pdo_update('jy_psipingtuan_goods',$update,array('id'=>$id));
	}
	exi('操作成功！');
}
if($_RQ['op']=='edit'){
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_psipingtuan_goods',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		$item['commissions']=$item['commissions']?json_decode($item['commissions'],true):array();
	}else{
		$item=array('get_type'=>0,'commissions'=>array());
	}
	$item['starttime']=$item['starttime']?date('Y-m-d H:i',$item['starttime']):'';
	$item['endtime']=$item['endtime']?date('Y-m-d H:i',$item['endtime']):'';
	$item['open_type']=$item['open_type']?:'0';
	$return['item']=$item;
	$commissions=array(
		array('name'=>'level_id','title'=>'适用等级','type'=>'select','help'=>'','options'=>'levels'),
		array('type'=>'string','name'=>'commissions.commission_0','title'=>'1级（直推）','help'=>'填写数字表示实际佣金金额，填写百分数表示比例。例如填写1表示发放1元佣金，填写1%表示发放商品价格1%的佣金'),
		array('type'=>'string','name'=>'commissions.commission_1','title'=>'2级（间推）','help'=>'填写数字表示实际佣金金额，填写百分数表示比例。例如填写1表示发放1元佣金，填写1%表示发放商品价格1%的佣金'),
		array('type'=>'string','name'=>'commissions.shangji','title'=>'上级（他的推荐人）','help'=>'自身复购奖励（非上级），填写数字表示实际佣金金额，填写百分数表示比例。例如填写1表示发放1元佣金，填写1%表示发放商品价格1%的佣金'),
		array('type'=>'string','name'=>'commissions.teamfee','title'=>'团队奖'),
		array('type'=>'string','name'=>'commissions.redfee','title'=>'合伙人分红'),
	);
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回商品列表'),
		),
		'edit_tab'=>true,
		'edits'=>array(
						array(
							'title'=>'基本信息',
							'list'=>array(
								array('type'=>'radio','title'=>'抽奖种类','name'=>'type','options'=>'gift_type','help'=>'根据商品可以选择：实物商品，虚拟卡密，虚拟币商品等！'),
								array('name'=>'goodsid','title'=>'选择商品','type'=>'choose_title','action'=>'jy_weishop/goods.get_goods//noweike=1','help'=>'根据商品可以选择：实物商品，虚拟卡密，虚拟币商品等！'),
								//array('name'=>'title','title'=>'商品名称','type'=>'string','show'=>array('type >='=>1)),
								//array('name'=>'image','title'=>'商品图片','type'=>'image','show'=>array('type >='=>1)),
								//array('name'=>'content','title'=>'商品内容','type'=>'richtext','show'=>array('type >='=>1)),
								//array('type'=>'unitstring','title'=>'奖品数量','unit'=>'个','name'=>'credit_fee','show'=>array('type'=>1)),
								array('type'=>'radio','title'=>'活动时间','name'=>'open_type','options'=>'open_type','help'=>'<red>未设置时间段的话，只有关闭了预约机制（设置--基本设置--关闭预约机制），才能正常参与拼团</red>'),
								array('name'=>'timeids','title'=>'拼团时间段','type'=>'checkbox_select','options'=>'times','help'=>'','show'=>array('open_type'=>0),'help'=>'<red>未设置时间段的话，只有关闭了预约机制（设置--基本设置--关闭预约机制），才能正常参与拼团</red>'),
								array('type'=>'unitstring','title'=>'拼团价格','name'=>'price','unit'=>'元'),
								array('type'=>'unitstring','title'=>'每人每周期最多N次的中奖','name'=>'maxzhong','unit'=>'次'),
								//array('type'=>'unitstring','title'=>'每人每月抽奖中满几次有额外奖','name'=>'zhongnum','unit'=>'次','help'=>''),
								array('type'=>'unitstring','title'=>'每人每周期中额外奖的概率','name'=>'otherzhong','unit'=>'%','help'=>'如果每人每月最多5次的中奖，就有一定的概率中额外一次奖励；例每个人每个月中6次的比例是10%，就是当月有100人已经中奖5次了，但是这100人里边最多只能再有10人可以再中1次'),
								array('type'=>'unitstring','title'=>'必须推荐N个人后才可能月中额外奖','name'=>'other_share','unit'=>'人'),
								array('type'=>'radio','title'=>'额外奖设置','name'=>'other_type','options'=>'gift_type','help'=>'根据商品可以选择：实物商品，虚拟卡密商品等'),
								array('name'=>'other_goodsid','title'=>'选择商品','type'=>'choose_title','action'=>'jy_weishop/goods.get_goods//noweike=1','show'=>array('other_type'=>0)),
								array('type'=>'unitstring','title'=>'额外奖数量','unit'=>'个','name'=>'other_credit_fee','show'=>array('other_type'=>array(1,2,3,4,5))),
								//array('type'=>'unitstring','title'=>'拼团定金','name'=>'ding_price','unit'=>'元'),
								array('type'=>'unitstring','title'=>'成团人数','name'=>'teamnum','unit'=>'人','help'=>''),
								array('type'=>'unitstring','title'=>'组团限时','name'=>'timeout','unit'=>'分钟','help'=>'仅填数字，单位:分钟'),
								array('type'=>'unitstring','title'=>'拼中人数','name'=>'lastnum','unit'=>'人','help'=>'从总成团人数中，随机抽取N人，抽中的拼团成功！'),
								array('type'=>'radio','title'=>'拼中奖返','name'=>'zhongfan_type','options'=>'credit_type'),
								array('type'=>'unitstring','title'=>'拼中奖','name'=>'zhongfan_fee','unit'=>'个'),
								array('type'=>'radio','title'=>'未中奖返','name'=>'fan_type','options'=>'fancredit_type'),
								array('type'=>'unitstring','title'=>'未中奖','name'=>'fan_fee','unit'=>'个','show'=>array('fan_type'=>array(1,2,3,4,5,6)),'help'=>'只有成团后，未中奖的才有奖励。如果拼团失败，所有人都没有奖励的！不然平台会亏损的'),
								array('type'=>'string_two','title'=>'未中奖返','title1'=>'返积分','unit1'=>'个','name1'=>'fan_fee1','title2'=>'返余额','unit2'=>'元','name2'=>'fan_fee2','show'=>array('fan_type'=>9)),
								array('type'=>'radio','title'=>'退款补偿奖','name'=>'tuifan_type','options'=>'credit_type','help'=>'如果拼中的人退货，未中奖返将退回，只能获得退款补偿奖'),
								array('type'=>'unitstring','title'=>'退款补偿奖','name'=>'tuifan_fee','unit'=>'个'),
								array('type'=>'unitstring','unit'=>'天','title'=>'周期天数','name'=>'pin_days','help'=>'默认7天，表示：7天内，最多拼中次数'),
								array('type'=>'unitstring','title'=>'团长返现','name'=>'teamfan_fee','unit'=>'元/人','help'=>'比如填30；就表示：团长自己买返30，其他2个团员参与，每个返团长30，就是团长一共90'),
								//array('type'=>'radio','title'=>'收货方式','name'=>'get_type','options'=>'get_type'),
							)
						),
						array(
							'title'=>'推荐佣金设置',
							'list'=>array(
								array('title'=>'[温馨提示]：
								</br>1、默认等级适合所有等级（当不匹配等级的时候，都按默认等级的比例算！）；
								</br>2、这里的设置优先于设置--基本设置的：《任务解锁》
								</br>3、设置--基本设置--《任务解锁》--否开启任务解锁功能后，才有分销
								','type'=>'tip'),
								array('type'=>'array','title'=>'佣金设置','name'=>'commissions','add_tip'=>'添加一个设置',
										'list'=>$commissions,
										'details'=>$commissions
								),
							)
						)
			
			)
	);
	$return['open_type']=$this->open_type;
	$return['times']=$this->times;
	$return['gift_type']=$this->gift_type;
	$return['credit_type']=$this->credit_type;
	$return['fancredit_type']=$this->fancredit_type;
	$return['get_type']=$this->get_types;
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	if(!$postdata['goodsid']){
		exi('请选择商品','error');
	}
	if(!$postdata['teamnum'] || $postdata['teamnum']<2){
		exi('成团人数必须大于等于2','error');
	}
	if($postdata['teamnum']<$postdata['lastnum']){
		exi('成团人数必须大于等于拼中人数','error');
	}
	if($postdata['lastnum']<=0){
		exi('拼中人数必填','error');
	}
	if($postdata['price']<=0){
		exi('拼团价格必填','error');
	}
	if($postdata['type']!=1 && !$postdata['title']&& !$postdata['image']){
		//exi('请设置商品名称,商品图片！','error');
	}
	if($postdata['timeout'] && $postdata['timeout']<10){
		exi('拼团时间不能小于10分钟','error');
	}
	if(!$postdata['timeids'] && !$postdata['open_type']){
		exi('拼团时间段必填！','error');
	}
	$data=array(
		'type'=>intval($postdata['type']),
		'title'=>trim($postdata['title']),
		'image'=>trim($postdata['image']),
		'content'=>trim($postdata['content']),
		'goodsid'=>intval($postdata['goodsid']),
		'timeout'=>intval($postdata['timeout']),
		'teamnum'=>intval($postdata['teamnum']),
		'lastnum'=>intval($postdata['lastnum']),
		'zhongfan_type'=>intval($postdata['zhongfan_type']),
		'zhongfan_fee'=>trim($postdata['zhongfan_fee']),
		'fan_type'=>intval($postdata['fan_type']),
		'fan_fee'=>trim($postdata['fan_fee']),
		'fan_fee1'=>trim($postdata['fan_fee1']),
		'fan_fee2'=>trim($postdata['fan_fee2']),
		'tuifan_type'=>intval($postdata['tuifan_type']),
		'tuifan_fee'=>trim($postdata['tuifan_fee']),
		'get_type'=>intval($postdata['get_type']),
		'ding_price'=>trim($postdata['ding_price']),
		'status'=>intval($postdata['status']),
		'paixu'=>intval($postdata['paixu']),
		'maxzhong'=>intval($postdata['maxzhong']),
		'otherzhong'=>intval($postdata['otherzhong']),
		'other_credit_fee'=>intval($postdata['other_credit_fee']),
		'other_type'=>intval($postdata['other_type']),
		'other_share'=>intval($postdata['other_share']),
		'other_goodsid'=>intval($postdata['other_goodsid']),
		'price'=>trim($postdata['price']),
		'credit_fee'=>intval($postdata['credit_fee']),
		'timeids'=>trim($postdata['timeids']),
		'open_type'=>intval($postdata['open_type']),
		'commissions'=>$postdata['commissions']?ijson_encode($postdata['commissions']):'',
		'createtime'=>TIMESTAMP,
		'pin_days'=>intval($postdata['pin_days']),
		'teamfan_fee'=>intval($postdata['teamfan_fee']),
	);
	$id=intval($postdata['id']);
	$isadd=pdo_get('jy_psipingtuan_goods',array('goodsid'=>intval($postdata['goodsid']),'uniacid'=>$_SESSION['uniacid']));
	if($id){
		if($isadd['id'] && $id !=$isadd['id']){
			exi('该商品已经参与了其他拼团活动！','error');
		}
		pdo_update('jy_psipingtuan_goods',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		if($isadd['id']){
			exi('该商品已经参与了其他拼团活动！','error');
		}
		$data['uniacid']=$_SESSION['uniacid'];
		pdo_insert('jy_psipingtuan_goods',$data);
	}
	exi('操作成功！','','list');
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_psipingtuan_goods',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择要编辑的商品！','error');
	}
	$post=get_postdata();
	if(isset($post['status'])){
		pdo_update('jy_psipingtuan_goods',array('status'=>intval($post['status'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	exi('操作成功！');
}
